home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_002 / microemacs / termio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  299 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6. #include    <stdio.h>
  7. #include    "ed.h"
  8.  
  9. #if    AMIGA
  10. #define NEW 1006
  11. #define    AMG_MAXBUF    1024
  12. static long terminal;
  13. static char    scrn_tmp[AMG_MAXBUF+1];
  14. static int    scrn_tmp_p = 0;
  15. #endif
  16.  
  17. #if    VMS
  18. #include    <stsdef.h>
  19. #include    <ssdef.h>
  20. #include    <descrip.h>
  21. #include    <iodef.h>
  22. #include    <ttdef.h>
  23.  
  24. #define NIBUF    128            /* Input buffer size        */
  25. #define NOBUF    1024            /* MM says bug buffers win!    */
  26. #define EFN    0            /* Event flag            */
  27.  
  28. char    obuf[NOBUF];            /* Output buffer        */
  29. int    nobuf;            /* # of bytes in above      */
  30. char    ibuf[NIBUF];            /* Input buffer         */
  31. int    nibuf;            /* # of bytes in above    */
  32. int    ibufi;            /* Read index            */
  33. int    oldmode[2];            /* Old TTY mode bits        */
  34. int    newmode[2];            /* New TTY mode bits        */
  35. short    iochan;             /* TTY I/O channel        */
  36. #endif
  37.  
  38. #if    CPM
  39. #include    <bdos.h>
  40. #endif
  41.  
  42. #if    MSDOS
  43. #undef    LATTICE
  44. #include    <dos.h>
  45. #endif
  46.  
  47. #if RAINBOW
  48. #include "rainbow.h"
  49. #endif
  50.  
  51. #if V7
  52. #include    <sgtty.h>        /* for stty/gtty functions */
  53. struct  sgttyb  ostate;         /* saved tty state */
  54. struct  sgttyb  nstate;         /* values for editor mode */
  55. #endif
  56.  
  57. /*
  58.  * This function is called once to set up the terminal device streams.
  59.  * On VMS, it translates SYS$INPUT until it finds the terminal, then assigns
  60.  * a channel to it and sets it raw. On CPM it is a no-op.
  61.  */
  62. ttopen()
  63. {
  64. #if    AMIGA
  65.     terminal = Open("RAW:1/1/639/199/MicroEmacs", NEW);
  66. #endif
  67. #if    VMS
  68.     struct  dsc$descriptor  idsc;
  69.     struct  dsc$descriptor  odsc;
  70.     char    oname[40];
  71.     int    iosb[2];
  72.     int    status;
  73.  
  74.     odsc.dsc$a_pointer = "SYS$INPUT";
  75.     odsc.dsc$w_length  = strlen(odsc.dsc$a_pointer);
  76.     odsc.dsc$b_dtype    = DSC$K_DTYPE_T;
  77.     odsc.dsc$b_class    = DSC$K_CLASS_S;
  78.     idsc.dsc$b_dtype    = DSC$K_DTYPE_T;
  79.     idsc.dsc$b_class    = DSC$K_CLASS_S;
  80.     do {
  81.         idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  82.         idsc.dsc$w_length  = odsc.dsc$w_length;
  83.         odsc.dsc$a_pointer = &oname[0];
  84.         odsc.dsc$w_length  = sizeof(oname);
  85.         status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  86.         if (status!=SS$_NORMAL && status!=SS$_NOTRAN)
  87.             exit(status);
  88.         if (oname[0] == 0x1B) {
  89.             odsc.dsc$a_pointer += 4;
  90.             odsc.dsc$w_length  -= 4;
  91.         }
  92.     } while (status == SS$_NORMAL);
  93.     status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  94.     if (status != SS$_NORMAL)
  95.         exit(status);
  96.     status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  97.               oldmode, sizeof(oldmode), 0, 0, 0, 0);
  98.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  99.         exit(status);
  100.     newmode[0] = oldmode[0];
  101.     newmode[1] = oldmode[1] | TT$M_PASSALL | TT$M_NOECHO;
  102.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  103.               newmode, sizeof(newmode), 0, 0, 0, 0);
  104.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  105.         exit(status);
  106. #endif
  107. #if    CPM
  108. #endif
  109. #if    MSDOS
  110. #endif
  111. #if    V7
  112.     gtty(1, &ostate);            /* save old state */
  113.     gtty(1, &nstate);            /* get base of new state */
  114.     nstate.sg_flags |= RAW;
  115.     nstate.sg_flags &= ~(ECHO|CRMOD);    /* no echo for now... */
  116.     stty(1, &nstate);            /* set mode */
  117. #endif
  118. }
  119.  
  120. /*
  121.  * This function gets called just before we go back home to the command
  122.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  123.  * Another no-operation on CPM.
  124.  */
  125. ttclose()
  126. {
  127. #if    AMIGA
  128.     amg_flush();
  129.     Close(terminal);
  130. #endif
  131. #if    VMS
  132.     int    status;
  133.     int    iosb[1];
  134.  
  135.     ttflush();
  136.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  137.          oldmode, sizeof(oldmode), 0, 0, 0, 0);
  138.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  139.         exit(status);
  140.     status = SYS$DASSGN(iochan);
  141.     if (status != SS$_NORMAL)
  142.         exit(status);
  143. #endif
  144. #if    CPM
  145. #endif
  146. #if    MSDOS
  147. #endif
  148. #if    V7
  149.     stty(1, &ostate);
  150. #endif
  151. }
  152.  
  153. /*
  154.  * Write a character to the display. On VMS, terminal output is buffered, and
  155.  * we just put the characters in the big array, after checking for overflow.
  156.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  157.  * MS-DOS (use the very very raw console output routine).
  158.  */
  159. ttputc(c)
  160. #if    AMIGA
  161.     char c;
  162. #endif
  163. {
  164. #if    AMIGA
  165.     scrn_tmp[scrn_tmp_p++] = c;
  166.     if(scrn_tmp_p>=AMG_MAXBUF)
  167.         amg_flush();
  168. #endif
  169. #if    VMS
  170.     if (nobuf >= NOBUF)
  171.         ttflush();
  172.     obuf[nobuf++] = c;
  173. #endif
  174.  
  175. #if    CPM
  176.     bios(BCONOUT, c, 0);
  177. #endif
  178.  
  179. #if    MSDOS & CWC86
  180.     dosb(CONDIO, c, 0);
  181. #endif
  182.  
  183. #if RAINBOW
  184.     Put_Char(c);            /* fast video */
  185. #endif
  186.  
  187. #if    V7
  188.     fputc(c, stdout);
  189. #endif
  190. }
  191.  
  192. amg_flush()
  193. {
  194.     if(scrn_tmp_p)
  195.         Write(terminal,scrn_tmp,scrn_tmp_p);
  196.     scrn_tmp_p = 0;
  197. }
  198.  
  199. /*
  200.  * Flush terminal buffer. Does real work where the terminal output is buffered
  201.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  202.  */
  203. ttflush()
  204. {
  205. #if    AMIGA
  206.     amg_flush();
  207. #endif
  208. #if    VMS
  209.     int    status;
  210.     int    iosb[2];
  211.  
  212.     status = SS$_NORMAL;
  213.     if (nobuf != 0) {
  214.         status = SYS$QIOW(EFN, iochan, IO$_WRITELBLK|IO$M_NOFORMAT,
  215.              iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
  216.         if (status == SS$_NORMAL)
  217.             status = iosb[0] & 0xFFFF;
  218.         nobuf = 0;
  219.     }
  220.     return (status);
  221. #endif
  222. #if    CPM
  223. #endif
  224. #if    MSDOS
  225. #endif
  226. #if    V7
  227.     fflush(stdout);
  228. #endif
  229. }
  230.  
  231. /*
  232.  * Read a character from the terminal, performing no editing and doing no echo
  233.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  234.  * simple on CPM, because the system can do exactly what you want.
  235.  */
  236. ttgetc()
  237. {
  238. #if    AMIGA
  239.     char ch;
  240.     amg_flush();
  241.     Read(terminal, &ch, 1);
  242.     return (int) ch;
  243. #endif
  244. #if    VMS
  245.     int    status;
  246.     int    iosb[2];
  247.     int    term[2];
  248.  
  249.     while (ibufi >= nibuf) {
  250.         ibufi = 0;
  251.         term[0] = 0;
  252.         term[1] = 0;
  253.         status = SYS$QIOW(EFN, iochan, IO$_READLBLK|IO$M_TIMED,
  254.              iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
  255.         if (status != SS$_NORMAL)
  256.             exit(status);
  257.         status = iosb[0] & 0xFFFF;
  258.         if (status!=SS$_NORMAL && status!=SS$_TIMEOUT)
  259.             exit(status);
  260.         nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  261.         if (nibuf == 0) {
  262.             status = SYS$QIOW(EFN, iochan, IO$_READLBLK,
  263.                  iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
  264.             if (status != SS$_NORMAL
  265.             || (status = (iosb[0]&0xFFFF)) != SS$_NORMAL)
  266.                 exit(status);
  267.             nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  268.         }
  269.     }
  270.     return (ibuf[ibufi++] & 0xFF);      /* Allow multinational  */
  271. #endif
  272.  
  273. #if    CPM
  274.     return (biosb(BCONIN, 0, 0));
  275. #endif
  276.  
  277. #if RAINBOW
  278.     int Ch;
  279.  
  280.     while ((Ch = Read_Keyboard()) < 0);
  281.  
  282.     if ((Ch & Function_Key) == 0)
  283.         if (!((Ch & 0xFF) == 015 || (Ch & 0xFF) == 0177))
  284.             Ch &= 0xFF;
  285.  
  286.     return Ch;
  287. #endif
  288.  
  289. #if    MSDOS & MWC86
  290.     return (dosb(CONRAW, 0, 0));
  291. #endif
  292.  
  293. #if    V7
  294.     return(fgetc(stdin));
  295. #endif
  296. }
  297.  
  298.  
  299.